package com.xiaoxx.controller.chat;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxx.annotation.LogRecords;
import com.xiaoxx.common.error.ErrorException;
import com.xiaoxx.common.result.R;
import com.xiaoxx.common.result.RType;
import com.xiaoxx.model.dto.chat.ChatApiKeyDto;
import com.xiaoxx.model.dto.chat.ChatModelDto;
import com.xiaoxx.model.dto.chat.ChatModelQueryDto;
import com.xiaoxx.model.vo.chat.ChatApiKeyVo;
import com.xiaoxx.model.vo.chat.ChatContentListVo;
import com.xiaoxx.model.vo.chat.ChatModelVo;
import com.xiaoxx.service.ChatApikeyService;
import com.xiaoxx.service.ChatModelService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HX
 * @since 2024-05-17
 */
@RestController
@RequestMapping("/api/chat/apikey")
public class ChatApikeyController {

    @Autowired
    private ChatApikeyService chatApikeyService;

    @ApiOperation("新增Apikey")
    @ApiImplicitParams({@ApiImplicitParam(name = "chatApiKeyDto", value = "新增Apikey", required = true, dataType = "ChatApiKeyDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @LogRecords(moduleName = "AI模型管理", content = "新增Apikey")
    public R<Boolean> add(@Valid @RequestBody ChatApiKeyDto chatApiKeyDto) {
        return R.success(chatApikeyService.addChatApikey(chatApiKeyDto));
    }

    @ApiOperation("修改Apikey")
    @ApiImplicitParams({@ApiImplicitParam(name = "chatApiKeyDto", value = "修改Apikey", required = true, dataType = "ChatApiKeyDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/upd", method = RequestMethod.POST)
    @LogRecords(moduleName = "AI模型管理", content = "修改Apikey")
    public R<Boolean> upd(@Valid @RequestBody ChatApiKeyDto chatApiKeyDto) {
        if (!StringUtils.hasLength(chatApiKeyDto.getId())) {
            throw new ErrorException(RType.PARAM_ID_REQUIRED_TRUE);
        }
        return R.success(chatApikeyService.updChatApikey(chatApiKeyDto));
    }

    @GetMapping("/list")
    @ApiOperation("key 列表")
    @ApiResponses(value = {@ApiResponse(response = ChatApiKeyVo.class, code = 200, message = "响应成功")})
    public R<List<ChatApiKeyVo>> list(@RequestParam(required = false) Integer status) {
        return R.success(chatApikeyService.getList(status));
    }

    @RequestMapping(value = "/updStatus", method = RequestMethod.GET)
    @ApiOperation(value = "1、使用中 2未使用", notes = "1、使用中 2未使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "status", required = true, paramType = "query")
    })
    @LogRecords(moduleName = "AI模型管理", content = "Apikey使用状态修改")
    public R<Boolean> updStatus(@RequestParam String id) {
        return R.success(chatApikeyService.updStatus(id));
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ApiOperation(value = "ID删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "删除", required = true, dataType = "String", paramType = "query")})
    @ApiResponses(value = {@ApiResponse(response = Boolean.class, code = 200, message = "响应成功")})
    @LogRecords(moduleName = "AI模型管理", content = "删除Apikey")
    public R<Boolean> del(@RequestParam String id) {
        return R.successDelete(chatApikeyService.del(id));
    }
}

